﻿#region Using Directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;

using Indeco.G24.Entities;
using Indeco.G24.Data;
using Indeco.G24.Data.Bases;
using Indeco.G24.Services;
#endregion

namespace Indeco.G24.Web.Data
{
	/// <summary>
	/// Represents the DataRepository.NguoiDungVaiTroProvider object that provides
	/// data to data-bound controls in multi-tier Web application architectures.
	/// </summary>
	[Designer(typeof(NguoiDungVaiTroDataSourceDesigner))]
	public class NguoiDungVaiTroDataSource : ProviderDataSource<NguoiDungVaiTro, NguoiDungVaiTroKey>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the NguoiDungVaiTroDataSource class.
		/// </summary>
		public NguoiDungVaiTroDataSource() : base(new NguoiDungVaiTroService())
		{
		}

		#endregion Constructors
		
		#region Properties
		
		/// <summary>
		/// Gets a reference to the NguoiDungVaiTroDataSourceView used by the NguoiDungVaiTroDataSource.
		/// </summary>
		protected NguoiDungVaiTroDataSourceView NguoiDungVaiTroView
		{
			get { return ( View as NguoiDungVaiTroDataSourceView ); }
		}
		
		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the NguoiDungVaiTroDataSource control invokes to retrieve data.
		/// </summary>
		public NguoiDungVaiTroSelectMethod SelectMethod
		{
			get
			{
				NguoiDungVaiTroSelectMethod selectMethod = NguoiDungVaiTroSelectMethod.GetAll;
				Object method = ViewState["SelectMethod"];
				if ( method != null )
				{
					selectMethod = (NguoiDungVaiTroSelectMethod) method;
				}
				return selectMethod;
			}
			set { ViewState["SelectMethod"] = value; }
		}

		#endregion Properties
		
		#region Methods

		/// <summary>
		/// Creates a new instance of the NguoiDungVaiTroDataSourceView class that is to be
		/// used by the NguoiDungVaiTroDataSource.
		/// </summary>
		/// <returns>An instance of the NguoiDungVaiTroDataSourceView class.</returns>
		protected override BaseDataSourceView<NguoiDungVaiTro, NguoiDungVaiTroKey> GetNewDataSourceView()
		{
			return new NguoiDungVaiTroDataSourceView(this, DefaultViewName);
		}
		
		/// <summary>
        /// Creates a cache hashing key based on the startIndex, pageSize and the SelectMethod being used.
        /// </summary>
        /// <param name="startIndex">The current start row index.</param>
        /// <param name="pageSize">The current page size.</param>
        /// <returns>A string that can be used as a key for caching purposes.</returns>
		protected override string CacheHashKey(int startIndex, int pageSize)
        {
			return String.Format("{0}:{1}:{2}", SelectMethod, startIndex, pageSize);
        }
		
		#endregion Methods
	}
	
	/// <summary>
	/// Supports the NguoiDungVaiTroDataSource control and provides an interface for
	/// data-bound controls to perform data operations with business and data objects.
	/// </summary>
	public class NguoiDungVaiTroDataSourceView : ProviderDataSourceView<NguoiDungVaiTro, NguoiDungVaiTroKey>
	{
		#region Declarations

		#endregion Declarations
		
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the NguoiDungVaiTroDataSourceView class.
		/// </summary>
		/// <param name="owner">A reference to the NguoiDungVaiTroDataSource which created this instance.</param>
		/// <param name="viewName">The name of the view.</param>
		public NguoiDungVaiTroDataSourceView(NguoiDungVaiTroDataSource owner, String viewName)
			: base(owner, viewName)
		{
		}
		
		#endregion Constructors
		
		#region Properties

		/// <summary>
		/// Gets a strongly-typed reference to the Owner property.
		/// </summary>
		internal NguoiDungVaiTroDataSource NguoiDungVaiTroOwner
		{
			get { return Owner as NguoiDungVaiTroDataSource; }
		}

		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the DataSource control invokes to retrieve data.
		/// </summary>
		internal NguoiDungVaiTroSelectMethod SelectMethod
		{
			get { return NguoiDungVaiTroOwner.SelectMethod; }
			set { NguoiDungVaiTroOwner.SelectMethod = value; }
		}

		/// <summary>
		/// Gets a strongly typed reference to the Provider property.
		/// </summary>
		internal NguoiDungVaiTroService NguoiDungVaiTroProvider
		{
			get { return Provider as NguoiDungVaiTroService; }
		}

		#endregion Properties
		
		#region Methods
		
		/// <summary>
		/// Gets a collection of Entity objects based on the value of the SelectMethod property.
		/// </summary>
		/// <param name="count">The total number of rows in the DataSource.</param>
		/// <returns>A collection of Entity objects.</returns>
		protected override IList<NguoiDungVaiTro> GetSelectData(out int count)
		{
			Hashtable values = CollectionsUtil.CreateCaseInsensitiveHashtable(GetParameterValues());
			Hashtable customOutput = CollectionsUtil.CreateCaseInsensitiveHashtable();
			IList<NguoiDungVaiTro> results = null;
			NguoiDungVaiTro item;
			count = 0;
			
			System.Guid iDVaiTro;
			System.Guid iDNguoiDung;

			switch ( SelectMethod )
			{
				case NguoiDungVaiTroSelectMethod.Get:
					NguoiDungVaiTroKey entityKey  = new NguoiDungVaiTroKey();
					entityKey.Load(values);
					item = NguoiDungVaiTroProvider.Get(entityKey);
					results = new TList<NguoiDungVaiTro>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				case NguoiDungVaiTroSelectMethod.GetAll:
                    results = NguoiDungVaiTroProvider.GetAll(StartIndex, PageSize, out count);
                    break;
				case NguoiDungVaiTroSelectMethod.GetPaged:
					results = NguoiDungVaiTroProvider.GetPaged(WhereClause, OrderBy, PageIndex, PageSize, out count);
					break;
				case NguoiDungVaiTroSelectMethod.Find:
					if ( FilterParameters != null )
						results = NguoiDungVaiTroProvider.Find(FilterParameters, OrderBy, StartIndex, PageSize, out count);
					else
						results = NguoiDungVaiTroProvider.Find(WhereClause, StartIndex, PageSize, out count);
                    break;
				// PK
				case NguoiDungVaiTroSelectMethod.GetByIDVaiTroIDNguoiDung:
					iDVaiTro = ( values["IDVaiTro"] != null ) ? (System.Guid) EntityUtil.ChangeType(values["IDVaiTro"], typeof(System.Guid)) : Guid.Empty;
					iDNguoiDung = ( values["IDNguoiDung"] != null ) ? (System.Guid) EntityUtil.ChangeType(values["IDNguoiDung"], typeof(System.Guid)) : Guid.Empty;
					item = NguoiDungVaiTroProvider.GetByIDVaiTroIDNguoiDung(iDVaiTro, iDNguoiDung);
					results = new TList<NguoiDungVaiTro>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				// IX
				// FK
				case NguoiDungVaiTroSelectMethod.GetByIDNguoiDung:
					iDNguoiDung = ( values["IDNguoiDung"] != null ) ? (System.Guid) EntityUtil.ChangeType(values["IDNguoiDung"], typeof(System.Guid)) : Guid.Empty;
					results = NguoiDungVaiTroProvider.GetByIDNguoiDung(iDNguoiDung, this.StartIndex, this.PageSize, out count);
					break;
				case NguoiDungVaiTroSelectMethod.GetByIDVaiTro:
					iDVaiTro = ( values["IDVaiTro"] != null ) ? (System.Guid) EntityUtil.ChangeType(values["IDVaiTro"], typeof(System.Guid)) : Guid.Empty;
					results = NguoiDungVaiTroProvider.GetByIDVaiTro(iDVaiTro, this.StartIndex, this.PageSize, out count);
					break;
				// M:M
				// Custom
				default:
					break;
			}

			if ( results != null && count < 1 )
			{
				count = results.Count;

				if ( !String.IsNullOrEmpty(CustomMethodRecordCountParamName) )
				{
					object objCustomCount = EntityUtil.ChangeType(customOutput[CustomMethodRecordCountParamName], typeof(Int32));
					
					if ( objCustomCount != null )
					{
						count = (int) objCustomCount;
					}
				}
			}
			
			return results;
		}
		
		/// <summary>
		/// Gets the values of any supplied parameters for internal caching.
		/// </summary>
		/// <param name="values">An IDictionary object of name/value pairs.</param>
		protected override void GetSelectParameters(IDictionary values)
		{
			if ( SelectMethod == NguoiDungVaiTroSelectMethod.Get || SelectMethod == NguoiDungVaiTroSelectMethod.GetByIDVaiTroIDNguoiDung )
			{
				EntityId = GetEntityKey(values);
			}
		}

		/// <summary>
		/// Sets the primary key values of the specified Entity object.
		/// </summary>
		/// <param name="entity">The Entity object to update.</param>
		protected override void SetEntityKeyValues(NguoiDungVaiTro entity)
		{
			base.SetEntityKeyValues(entity);
			
			// make sure primary key column(s) have been set
			if ( entity.IDVaiTro == Guid.Empty )
				entity.IDVaiTro = Guid.NewGuid();
			if ( entity.IDNguoiDung == Guid.Empty )
				entity.IDNguoiDung = Guid.NewGuid();
		}
		
		/// <summary>
		/// Performs a DeepLoad operation for the current entity if it has
		/// not already been performed.
		/// </summary>
		internal override void DeepLoad()
		{
			if ( !IsDeepLoaded )
			{
				NguoiDungVaiTro entity = GetCurrentEntity();
				
				if ( entity != null )
				{
					// init transaction manager
					GetTransactionManager();
					// execute deep load method
					NguoiDungVaiTroProvider.DeepLoad(GetCurrentEntity(), EnableRecursiveDeepLoad);
					// set loaded flag
					IsDeepLoaded = true;
				}
			}
		}

		/// <summary>
		/// Performs a DeepLoad operation on the specified entity collection.
		/// </summary>
		/// <param name="entityList"></param>
		/// <param name="properties"></param>
		internal override void DeepLoad(TList<NguoiDungVaiTro> entityList, ProviderDataSourceDeepLoadList properties)
		{
			// init transaction manager
			GetTransactionManager();
			// execute deep load method
			NguoiDungVaiTroProvider.DeepLoad(entityList, properties.Recursive, properties.Method, properties.GetTypes());
		}

		#endregion Select Methods
	}
	
	#region NguoiDungVaiTroDataSourceDesigner

	/// <summary>
	/// Provides design-time support in a design host for the NguoiDungVaiTroDataSource class.
	/// </summary>
	public class NguoiDungVaiTroDataSourceDesigner : ProviderDataSourceDesigner<NguoiDungVaiTro, NguoiDungVaiTroKey>
	{
		/// <summary>
		/// Initializes a new instance of the NguoiDungVaiTroDataSourceDesigner class.
		/// </summary>
		public NguoiDungVaiTroDataSourceDesigner()
		{
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public NguoiDungVaiTroSelectMethod SelectMethod
		{
			get { return ((NguoiDungVaiTroDataSource) DataSource).SelectMethod; }
			set { SetPropertyValue("SelectMethod", value); }
		}

		/// <summary>Gets the designer action list collection for this designer.</summary>
		/// <returns>The <see cref="T:System.ComponentModel.Design.DesignerActionListCollection"/>
		/// associated with this designer.</returns>
		public override DesignerActionListCollection ActionLists
		{
			get
			{
				DesignerActionListCollection actions = new DesignerActionListCollection();
				actions.Add(new NguoiDungVaiTroDataSourceActionList(this));
				actions.AddRange(base.ActionLists);
				return actions;
			}
		}
	}

	#region NguoiDungVaiTroDataSourceActionList

	/// <summary>
	/// Supports the NguoiDungVaiTroDataSourceDesigner class.
	/// </summary>
	internal class NguoiDungVaiTroDataSourceActionList : DesignerActionList
	{
		private NguoiDungVaiTroDataSourceDesigner _designer;

		/// <summary>
		/// Initializes a new instance of the NguoiDungVaiTroDataSourceActionList class.
		/// </summary>
		/// <param name="designer"></param>
		public NguoiDungVaiTroDataSourceActionList(NguoiDungVaiTroDataSourceDesigner designer) : base(designer.Component)
		{
			_designer = designer;
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public NguoiDungVaiTroSelectMethod SelectMethod
		{
			get { return _designer.SelectMethod; }
			set { _designer.SelectMethod = value; }
		}

		/// <summary>
		/// Returns the collection of <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// objects contained in the list.
		/// </summary>
		/// <returns>A <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// array that contains the items in this list.</returns>
		public override DesignerActionItemCollection GetSortedActionItems()
		{
			DesignerActionItemCollection items = new DesignerActionItemCollection();
			items.Add(new DesignerActionPropertyItem("SelectMethod", "Select Method", "Methods"));
			return items;
		}
	}

	#endregion NguoiDungVaiTroDataSourceActionList
	
	#endregion NguoiDungVaiTroDataSourceDesigner
	
	#region NguoiDungVaiTroSelectMethod
	
	/// <summary>
	/// Enumeration of method names available for the NguoiDungVaiTroDataSource.SelectMethod property.
	/// </summary>
	public enum NguoiDungVaiTroSelectMethod
	{
		/// <summary>
		/// Represents the Get method.
		/// </summary>
		Get,
		/// <summary>
		/// Represents the GetAll method.
		/// </summary>
		GetAll,
		/// <summary>
		/// Represents the GetPaged method.
		/// </summary>
		GetPaged,
		/// <summary>
		/// Represents the Find method.
		/// </summary>
		Find,
		/// <summary>
		/// Represents the GetByIDVaiTroIDNguoiDung method.
		/// </summary>
		GetByIDVaiTroIDNguoiDung,
		/// <summary>
		/// Represents the GetByIDNguoiDung method.
		/// </summary>
		GetByIDNguoiDung,
		/// <summary>
		/// Represents the GetByIDVaiTro method.
		/// </summary>
		GetByIDVaiTro
	}
	
	#endregion NguoiDungVaiTroSelectMethod

	#region NguoiDungVaiTroFilter
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilter&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="NguoiDungVaiTro"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class NguoiDungVaiTroFilter : SqlFilter<NguoiDungVaiTroColumn>
	{
	}
	
	#endregion NguoiDungVaiTroFilter

	#region NguoiDungVaiTroExpressionBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlExpressionBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="NguoiDungVaiTro"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class NguoiDungVaiTroExpressionBuilder : SqlExpressionBuilder<NguoiDungVaiTroColumn>
	{
	}
	
	#endregion NguoiDungVaiTroExpressionBuilder	

	#region NguoiDungVaiTroProperty
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ChildEntityProperty&lt;NguoiDungVaiTroChildEntityTypes&gt;"/> class
	/// that is used exclusively with a <see cref="NguoiDungVaiTro"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class NguoiDungVaiTroProperty : ChildEntityProperty<NguoiDungVaiTroChildEntityTypes>
	{
	}
	
	#endregion NguoiDungVaiTroProperty
}

